<?php

namespace App\Http\BackendControllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;


class LoginController extends Controller
{
    public function index()
    {

        if (Auth::guard('admin')->check()) {
            return redirect('/backend/home');
        }
        return view('backend.login.index');
    }

    public function login(Request $request)
    {
        $data = $request->only('account', 'password');
        $rules = [
            'account' => 'required',
            'password' => 'required',
        ];
        $message = [
            "account.required" => "请输入用户名",
            "password.required" => "请输入密码",
        ];
        $validator = Validator::make($data, $rules, $message);
        if ($validator->fails()) {
            return err('', $validator->errors()->first());
        }

        if (\Auth::guard('admin')->attempt($data)) {
            return res('','登录成功');
        } else {
            return err('', '账号密码不匹配');
        }
    }

    public function logout()
    {
        \Auth::guard('admin')->logout();
        return redirect('/backend/login');
    }


}
